Page({
  data: {
    isLiked: false,
    posts: [


    ],        // 帖子列表
    isLoading: false, // 是否正在加载数据
    page: 1,          // 当前页数
    hasMore: true     // 是否还有更多数据
  },

  onLoad() {

  },

  delIt(e) {
    const item = e.currentTarget.dataset.item
    if (item.userId !== wx.getStorageSync('user').id) {
      wx.showToast({
        title: '您无权操作！',
        icon: 'none'
      })
      return
    }

    wx.showModal({
      title: '提示',
      content: '是否删除当前动态？',

    }).then(async r => {
      if (r.confirm) {
        await getApp().$request('/api/delPost', 'POST', { id: item.id })
        wx.showToast({
          title: '删除成功',
        })
        this.loadPosts()
      }
    })
  },
  showIt(e) {
    const all = e.currentTarget.dataset.all
    const index = e.currentTarget.dataset.index
    wx.previewImage({
      urls: all,
      current: all[index]
    })
  },
  async likeIt(e) {
    const postId = e.currentTarget.dataset.postid;
    const index = e.currentTarget.dataset.index;
    const res = await getApp().$request('/api/likeIt', 'POST', { postId: postId, userId: wx.getStorageSync('user').id })
    // let posts = this.data.posts
    // posts[index].like ++
    // posts[index].isLiked = true
    // this.setData({posts})
    this.loadPosts()
  },
  // 加载帖子列表
  async loadPosts() {


    this.setData({ isLoading: true });
    const res = await getApp().$request('/api/getPostList', 'POST', { userId: wx.getStorageSync('user').id })

    this.setData({
      posts: res.data,
      isLoading: false
    })
    console.log(res)

  },

  // 跳转到帖子详情页面
  goToPostDetail(e) {
    console.log(e)
    const postId = e.currentTarget.dataset.postid;
    wx.navigateTo({
      url: `/pages/postDetail/postDetail?postId=${postId}`
    });
  },

  onShow() {
    this.loadPosts();
  },
  // 上拉加载更多
  onReachBottom() {
    this.loadPosts();
  }
});
